<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/20 09:35
 */

namespace App\Project\Filters;

class PastHistoryFilter extends FilterAbstract
{
    protected function forExtend()
    {
        $map = [
            'fat.time'                       => 1,
            'fat.weight'                     => 1,
            'fat.duration'                   => 1,
            'fat.drugs'                      => 1,
            'hypertension.time'              => 1,
            'hypertension.drugs'             => 1,
            'coronary_disease.time'          => 1,
            'coronary_disease.cabg'          => 1,
            'coronary_disease.drugs'         => 1,
            'hyperuricemia.time'             => 1,
            'hyperuricemia.drugs'            => 1,
            'hyperlipidemia.time'            => 1,
            'hyperlipidemia.drugs'           => 1,
            'fatty_liver.time'               => 1,
            'fatty_liver.drugs'              => 1,
            'hepatitis.type'                 => 1,
            'hepatitis.duration'             => 1,
            'hepatitis.drugs'                => 1,
            'pancreatitis.acute'             => 1,
            'pancreatitis.chronic'           => 1,
            'pancreatitis.drugs'             => 1,
            'pancreatic_operation.time'      => 1,
            'pancreatic_operation.operation' => 1,
            'renal.time'                     => 1,
            'renal.drugs'                    => 1,
            'cerebral_infarction.time'       => 1,
            'cerebral_infarction.drugs'      => 1,
            'transfusion.times'              => 1,
            'irritability.items'             => 1,
            'special_irritability.items'     => 1,
            'adverse_drug_reaction.items'    => 1,
            'adverse_drug_reaction.symptoms' => 1,
            'thyroid.type'                   => 1,
            'thyroid.time'                   => 1,
            'thyroid.drugs'                  => 1,
            'trauma.time'                    => 1,
            'trauma.drugs'                   => 1,
            'contagion.time'                 => 1,
            'contagion.drugs'                => 1,
            'other.time'                     => 1,
            'other.drugs'                    => 1,
            'pcos.time'                      => 1,
            'pcos.drugs'                     => 1,
        ];

        $map['extra']      = $this->extra();
        $map['medication'] = $this->schedule();
        $map['operation']  = $this->operation();

        return array_merge($map, $this->drugs());
    }

    protected function schedule()
    {
        $map = [];

        $count = count($this->request->input('medication'));

        for ($i = 0; $i < $count; $i++) {
            $map[$i] = array_flip(
                [
                    'name',
                    'dosage.type',
                    'dosage.value',
                    'dosage.values.morning',
                    'dosage.values.atnoon',
                    'dosage.values.evening',
                    'dosage.values.before_sleep',
                    'dosage.unit',
                    'start_time',
                    'end_time',
                    'usage',
                    'frequency',
                    'remark'
                ]);
        }

        return $map;
    }

    protected function extra()
    {
        $extras = $this->request->input('extra');

        if ($extras) {
            $map = [];
            foreach ($extras as $key => $extra) {
                $map[$key]['name']   = 1;
                $map[$key]['time']   = 1;
                $map[$key]['remark'] = 1;
                if (isset($extra['drugs']) && !empty($extra['drugs'])) {
                    $drugs = $extra['drugs'];
                    foreach ($drugs as $drug) {
                        $map[$key]['drugs'][] = 1;
                    }
                }
            }

            return $map;
        } else {
            return ['extra' => 1];
        }
    }

    protected function drugs()
    {
        $map = [];

        $hasDrugs = [
            'fat',
            'hypertension',
            'coronary_disease',
            'hyperuricemia',
            'hyperlipidemia',
            'fatty_liver',
            'hepatitis',
            'pancreatitis',
            'renal',
            'cerebral_infarction',
            'thyroid',
            'trauma',
            'contagion',
            'other',
            'pcos'
        ];

        foreach ($hasDrugs as $key) {

            if ($count = count($this->request->input($key . '.drugs'))) {
                $map[$key]['drugs'] = array_pad([], $count, 1);
            }
        }

        if ($count = count($this->request->input('coronary_disease.stenting'))) {
            $map['coronary_disease']['stenting'] = array_pad([], $count, 1);
        }

        return $map;
    }

    protected function operation()
    {
        $map = [];

        $operation = $this->request->input('operation');

        if (is_array($operation) && count($operation) > 0) {

            foreach ($operation as $key => $item) {
                $map[$key]['time'] = 1;
                $map[$key]['name'] = 1;
            }
        }

        return $map;
    }

    protected function getRootFieldsHasNoSon()
    {
        return [
            'id', 'record_id', 'fat', 'hypertension', 'coronary_disease', 'hyperuricemia', 'hyperlipidemia',
            'fatty_liver', 'hepatitis', 'pancreatitis',
            'pancreatic_operation', 'renal', 'cerebral_infarction', 'transfusion', 'irritability',
            'special_irritability',
            'adverse_drug_reaction', 'thyroid', 'medication', 'other', 'operation', 'remark', 'pcos'
        ];
    }

    protected function getRootFieldsHasSon()
    {
        return [];
    }

    protected function getSonFieldsForEachRoot()
    {
        return [];
    }
}